/**
 * 测试用例添加接口测试用例
 *
 * Created by raquelmao on 2020/06/10.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));

const common = require('./../../common');
const Administrator = require('./../../../models/administrator');
const uri = require('./../uri');
const benchmark = require('./../../../benchmark/generate-tests/self_designed');
const data = require('./../../../benchmark/test-cases/data');

module.exports = describe('/POST functions/:id/tests.json', function () {
  const administrator = new Administrator();
  let project = {};
  let projectVersion;
  let testFunc;
  const newUserIds = [];
  function url(functionId) {
    return `/functions/${functionId}/tests.json`;
  }

  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newUserIds.push(tester.userId);
        return tester.createRandomProject();
      })
      .then((res) => {
        project = res;
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'post');
  common.should.be.tester(uri, url, 'post');

  // 正常发起请求
  it(`it should add tests for ${benchmark.name} by data`, function (done) {
    let allFunctions;
    project.createRandomVersion()
      .then((res) => {
        projectVersion = res;
        return projectVersion.getAllFunctions(benchmark);
      })
      .then((res) => {
        allFunctions = res;
        return project.creator.login();
      })
      .then(() => {
        const promises = [];
        allFunctions.forEach((func) => {
          testFunc = func;
          promises.push(new Promise((resolve) => {
            let testCount = 0;
            projectVersion.getTests(func.functionId)
              .then((tests) => {
                testCount = tests.length;
                chai.request(uri)
                  .post(url(func.functionId))
                  .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
                  .send({ data: JSON.stringify(data), description: '123' })
                  .end((err, res) => {
                    res.should.have.status(200);
                    projectVersion.getTests(func.functionId)
                      .then((tests) => {
                        (tests.length - testCount).should.be.eql(1); // 导入的data文件中只有一个测试用例
                        resolve();
                      })
                      .catch(done);
                  });
              })
              .catch(done);
          }));
        });
        return Promise.all(promises);
      })
      .then(() => {
        done();
      })
      .catch(done);
  });

  it(`it should add tests for ${benchmark.name} by copyId`, function (done) {
    projectVersion.getTests(testFunc.functionId)
      .then((tests) => {
        const testCount = tests.length;
        chai.request(uri)
          .post(url(testFunc.functionId))
          .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
          .send({ copyId: tests[0].testId })
          .end((err, res) => {
            res.should.have.status(200);
            projectVersion.getTests(testFunc.functionId)
              .then((tests) => {
                (tests.length - testCount).should.be.eql(1);
                done();
              });
          });
      })
      .catch(done);
  });

  // 编组下新建测试用例
  it(`it should add tests for ${benchmark.name} in group by data`, function (done) {
    testFunc.addTestCaseGroup()
      .then((group) => {
        group.getTeseCaseGroupChildren()
          .then((tests) => {
            const testCount = tests.length;
            // creat testCase in the group
            chai.request(uri)
              .post(url(testFunc.functionId))
              .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
              .send({ data: {}, groupId: group.groupId })
              .end((err, res) => {
                res.should.have.status(200);
                group.getTeseCaseGroupChildren()
                  .then((tests) => {
                    (tests.length - testCount).should.be.eql(1);

                    done();
                  })
                  .catch(done);
              });
          })
          .catch(done);
      })
      .catch(done);
  });
});
